#include "XXborder.h"
#include "ui_XXborder.h"
#include "../XXqtDefine.h"
#include <functional>
#include <mutex>

#include "../Utility/XXlayoutConstraint.h"

XXborder::XXborder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::XXborder)
{
    ui->setupUi(this);

    // self init
    setAttribute(Qt::WA_StyledBackground);
    //setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlag(Qt::FramelessWindowHint);

    // field init
    _customContent = nullptr;
}
XXborder::~XXborder(){
    delete ui;
}

// Title Bar
XXtitleBar* XXborder::titleBar(){
    return ui->widget_titleBar;
}

// content
void XXborder::setContentUnderTitleBar(){
    ui->frame_background_layout->removeWidget(ui->widget_titleBar);
    XXlayoutConstraint::constraint(ui->widget_titleBar,XXlayoutConstraint::Attribute::RoundTop,ui->frame_background,XXlayoutConstraint::Attribute::RoundTop,0);
}
void XXborder::replaceContent(QWidget *widget){
    QBoxLayout *layout = qobject_cast<QBoxLayout*>(ui->frame_background->layout());
    if(nullptr == layout){
        return;
    }

    if(ui->widget_content){
        delete ui->widget_content;
        ui->widget_content = nullptr;
    }
    else if(_customContent){
        delete _customContent;
        _customContent = widget;
    }
    else{

    }

    widget->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Expanding);
    layout->addWidget(widget);
    ui->widget_titleBar->raise();
}
